package com.ecoolex.discount.sales.api.graphql;

import com.ecoolex.discount.sales.entity.*;
import com.ecoolex.discount.sales.enums.DiscountProductDiscountMode;
import com.ecoolex.discount.sales.pojo.QueryOnlineDiscountProductDTO;
import com.ecoolex.discount.sales.ws.DiscountProductService;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

@GraphQL2Module(type = GraphQL2Module.Type.User, description = "折扣产品")
public class DiscountProductGraphql {

    @Autowired
    private StorageService storageService;
    @Reference
    private DiscountProductService discountProductService;

    @TypeField(type = DiscountProductWithBLOBs.class, field = "brand", description = "品牌")
    public DiscountBrandWithBLOBs brand(@Source DiscountProductWithBLOBs source) {
        return discountProductService.getDiscountBrand(source.getBrandId());
    }

    @TypeField(type = DiscountProductWithBLOBs.class, field = "image", description = "产品图片")
    public StorageInfo image(@Source DiscountProductWithBLOBs source) {
        return storageService.get(source.getImage());
    }
    
    @TypeField(type = DiscountProductWithBLOBs.class, field = "attachList", description = "附加产品列表")
    public List<DiscountProductAttach> attachList(@Source DiscountProductWithBLOBs source){
    	return discountProductService.listByMainProductId(source.getProductId());
    }
    
    @TypeField(type = DiscountProductAttach.class, field = "discountProduct", description = "折扣产品")
    public DiscountProductWithBLOBs discountProduct(@Source DiscountProductAttach source){
    	return discountProductService.getProductById(source.getAttachProductId());
    }

    @TypeField(type = DiscountProductWithBLOBs.class, field = "giftCard", description = "礼品卡信息")
    public DiscountProductGiftCard giftCard(@Source DiscountProductWithBLOBs source) {
        if (DiscountProductDiscountMode.GIFT_CARD.equal(source.getDiscountMode())) {
            return discountProductService.getGiftCardByProductId(source.getProductId());
        } else {
            return null;
        }
    }

    @TypeField(type = DiscountProductWithBLOBs.class, field = "vouchers", description = "抵用券信息")
    public DiscountProductVouchers vouchers(@Source DiscountProductWithBLOBs source) {
        if (DiscountProductDiscountMode.VOUCHERS.equal(source.getDiscountMode())) {
            return discountProductService.getVouchersByProductId(source.getProductId());
        } else {
            return null;
        }
    }

    @TypeField(type = DiscountProductWithBLOBs.class, field = "recharge", description = "充值信息")
    public DiscountProductRecharge recharge(@Source DiscountProductWithBLOBs source) {
        if (DiscountProductDiscountMode.VOUCHERS.equal(source.getDiscountMode())) {
            return discountProductService.getRechargeByProductId(source.getProductId());
        } else {
            return null;
        }
    }

    @QueryField(field = "discountProduct", description = "折扣产品详情")
    public DiscountProductWithBLOBs queryProductByOnline(@NonNull Integer productId) {
        return discountProductService.getOnlineProductById(productId);
    }

    @QueryField(field = "discountProductList", description = "折扣产品列表")
    public Pagination<DiscountProductWithBLOBs> queryProductByOnline(@NonNull QueryOnlineDiscountProductDTO query, @NonNull Page page) {
        return discountProductService.queryProductByOnline(query, page);
    }

}
